package cn.exrick.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import cn.exrick.mapper.TbCollectionMapper;
import cn.exrick.pojo.TbCollection;
import cn.exrick.service.CollectionService;
import cn.exrick.utils.mybatis.Criteria;

@Service
public class CollectionServiceImpl extends BaseServiceImpl<TbCollection, TbCollectionMapper> implements CollectionService {
    private final static Logger log = LoggerFactory.getLogger(CollectionServiceImpl.class);

    @Override
    public Logger getLogger() {
        return log;
    }

    public Criteria createCriteria(Map<String, Object> params) {
        Criteria criteria = createCriteria();
        if (params.get("itemId") != null) {
            criteria.andFieldEqualTo("item_id", String.valueOf((Long) params.get("itemId")));
        }
        if (params.get("memberId") != null) {
            criteria.andFieldEqualTo("member_id", String.valueOf((Long) params.get("memberId")));
        }
        return criteria;
    }

    @Autowired
    TbCollectionMapper tbCollectionMapper;

    @Override
    public TbCollectionMapper getMapper() {
        return tbCollectionMapper;
    }

    public TbCollection findByItemAndMember(long itemId, long memberId) {
        List<TbCollection> list = findListByCriteria(new HashMap<String, Object>() {{
            this.put("itemId", itemId);
            this.put("memberId", memberId);
        }});
        if (list == null || list.size() <= 0) {
            return null;
        } else {
            return list.get(0);
        }
    }
}
